package com.csnf.hymall.dao.imp;

import com.csnf.hymall.dao.AdminMapper;
import com.csnf.hymall.entity.Admin;
import com.csnf.hymall.util.PageUtil;

import java.sql.SQLException;
import java.util.List;

public class AdminMapperImp extends BaseMapperImp implements AdminMapper {

    @Override
    public Integer insertOne(Admin admin) {
        String sql = " INSERT admin(admin_id,admin_name,admin_nickname,admin_password,admin_profile_picture_src)" +
                " VALUES (null,?,?,?,?,?)";
        try {
            return add(sql, admin.getAdmin_name(), admin.getAdmin_nickname(), admin.getAdmin_password(), admin.getAdmin_profile_picture_src());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer updateOne(Admin admin) {
        return null;
    }

    @Override
    public List<Admin> select(String admin_name, PageUtil pageUtil) {
        String sql ="  SELECT admin_id,admin_name,admin_nickname,admin_profile_picture_src FROM admin";
        sql+=" WHERE 1=1";
        if(null!=admin_name){
            sql+=" AND admin_name like concat('%',?,‘%’) ";
        }
        if(null!=pageUtil){
            sql+=" LIMIT "+pageUtil.getPageStart()+","+pageUtil.getCount();
        }
        try {
            return getList(sql, Admin.class, admin_name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Admin selectOne(String admin_name, Integer admin_id) {
        String sql ="SELECT admin_id,admin_name,admin_nickname,admin_profile_picture_src FROM admin";
        sql+=" WHERE 1=1";
        if(null!=admin_name){
            sql+=" AND admin_name = ? ";
        }
        if(null!=admin_id){
            sql+=" AND admin_id= ? ";
        }
        try {
            return get(sql, Admin.class, admin_name,admin_id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Admin selectByLogin(String admin_name, String admin_password) {
        String sql ="SELECT admin_id FROM admin";
        sql+=" WHERE 1=1";
        if(null!=admin_name && null!=admin_password){
            sql+=" AND admin_name = ? AND admin_password=? ";
        }else{
            return null;
        }
        try {
            return get(sql, Admin.class, admin_name,admin_password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer selectTotal(String admin_name) {
        String sql ="SELECT count(*) FROM admin";
        sql+=" WHERE 1=1";
        if(null!=admin_name){
            sql+=" AND admin_name like concat('%',?,‘%’) ";
        }
        try {
            return getTotalCount(sql, admin_name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
